<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\BaseController;
use App\Models\Client;
use App\Models\Event;
use App\Models\EventEmployee;
use App\Models\TouristEvent;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class EventController extends BaseController
{
    public function  getEvent(Request $request){
        $event =  Event::select(['events.id','events.name','events.type','events.address','events.date','events.permission','events.status'])
            //分页
            ->simplePaginate(5);

        return $this->returnJson(BaseController::OK,'success',$event);
    }

    public function createEvent(Request  $request){
        $info = $request->only('name','type','photo','address','date','client_id','employee_ids','permission');

        //判断必填参数是否为空
        if (empty($info['name']) || empty($info['type'])|| empty($info['photo'])|| empty($info['address'])|| empty($info['date'])|| empty($info['client_id'])|| empty($info['employee_ids'])|| empty($info['permission'])){
            return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY, 'data cannot be processed');
        }

        $info['type'] == 'course' ?$info['type'] = 0 : $info['type'] = 1 ;

        foreach ($info['employee_ids'] as $key){
            $users_info = User::where('id',$key)
                ->first(['scope']);

            //该员工是否存在
            if (empty($users_info)) return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY,'data cannot be processed1','');
            //类型是否匹配
            if ($users_info['scope'] != $info['type']) return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY,'data cannot be processed2','');
        }

        //判断是否有该客户
        $client = Client::where('id',$info['client_id'])
            ->get()
            ->toArray();

        if (empty($client))return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY,'data cannot be processed3','');
        //上传照片
        $url = $this->uploadFile(Request::capture(),'photo','events');

        if (!$url)return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY,'data cannot be processed4','');

        $code  = strtoupper(substr(md5(time()),0,8));

        //创建启动事务
        DB::beginTransaction();
        try {
            $event_id = Event::insertGetId([
                'name' => $info['name'],
                'type' => $info['type'],
                'date'  => $info['date'],
                'photo' => $_SERVER['HTTP_HOST'] . '/' . $url,
                'code' => $code,
                'address' => $info['address'],
                'client_id' => $info['client_id'],
                'permission' => $info['permission'],
            ]);

            foreach ($info['employee_ids'] as $key){
                EventEmployee::insert([
                    'event_id' => $event_id,
                    'user_id' => $key,
                    'status' => 0
                ]);
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY,'data cannot be processed','');
        }

        return $this->returnJson(BaseController::OK, 'success', $event_id);
    }

    public function  updateEmployees( Request $request,$event_id){
        $employee_ids = $request->get('employee_ids');

        //判断必填参数是否为空
        if (empty($event_id) || empty($employee_ids)){
            return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY, 'data cannot be processed');
        }

        $employee_id= EventEmployee::where('event_id',$event_id)->get(['id'])->toArray();

        $event_type= Event::where('id',$event_id)->pluck('type')->first();

        if (empty($employee_id)) return $this->returnJson(BaseController::NOTFOUND,'not found','');

        //创建启动事务
        DB::beginTransaction();
        try {
            foreach ($employee_id as $key){
                EventEmployee::where('id' , $key['id']) ->delete();
            }
            foreach ($employee_ids as $key){
                $users_info = User::where('id',$key)
                    ->first(['scope']);

                //该员工是否存在
                if (empty($users_info)) return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY,'data cannot be processed1','');
                //类型是否匹配
                if ($users_info['scope'] != $event_type[0]) return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY,'data cannot be processed2','');

                EventEmployee::insert([
                    'event_id' => $event_id,
                    'user_id' => $key,
                    'status' => 0
                ]);
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY,'data cannot be processed','');
        }

        return $this->returnJson(BaseController::OK,'success','');
    }

    public function  updateClient( Request $request,$event_id){
        $client_id = $request->get('client_id');

        //判断必填参数是否为空
        if (empty($event_id) || empty($client_id)){
            return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY, 'data cannot be processed');
        }

        $client = Client::where('id',$client_id)->get()->toArray();

        if (empty($client)) return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY,'1data cannot be processed','');

        try {
            Event::where('id',$event_id)->update([
                'client_id' => $client_id
            ]);
        }catch (\Exception $e) {
            return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY,'2data cannot be processed','');
        }

        return $this->returnJson(BaseController::OK,'success','');
    }

    public function  deleteEvent( Request $request,$event_id){

        //判断必填参数是否为空
        if (empty($event_id)){
            return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY, 'data cannot be processed');
        }

        //创建启动事务
        DB::beginTransaction();
        try {
            EventEmployee::where('event_id' , $event_id) ->delete();

            Event::where('id' , $event_id) ->delete();

            TouristEvent::where('event_id' , $event_id) ->delete();

            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->returnJson(BaseController::NOTFOUND,'not found','');
        }

        return $this->returnJson(BaseController::OK,'success','');
    }

}
